package com.dymall.order.controller;

import com.dymall.common.exception.BadRequestException;
import com.dymall.order.domain.dto.OrderFormDTO;
import com.dymall.order.domain.po.Order;
import com.dymall.order.domain.vo.OrderVO;
import com.dymall.order.service.IOrderService;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: chakgai
 * @Description: 订单管理接口
 * @DateTime: 2025/2/13 22:39
 **/
@RestController
@RequestMapping("/orders")
@RequiredArgsConstructor
public class OrderController {
    private final IOrderService orderService;

    /**
     * 创建订单
     * @param orderFormDTO
     * @return
     */
    @PostMapping("/create")
    public Long createOrder(@RequestBody OrderFormDTO orderFormDTO){
        return orderService.createOrder(orderFormDTO);
    }

    /**
     * 根据id展示订单
     * @param orderId
     * @return
     */
    @GetMapping("{id}")
    public OrderVO queryOrderById(@Param("订单id")@PathVariable("id") Long orderId) {
        Order order = orderService.getById(orderId); // 根据订单ID查询订单
        if (order == null) {
            throw new BadRequestException("订单不存在");
        }
        // 使用 BeanUtils.copyProperties 进行转换
        OrderVO orderVO = new OrderVO();
        BeanUtils.copyProperties(order, orderVO);
        return orderVO;
    }

    /**
     * 标记订单为已支付
     * @param orderId
     * @return
     */
    @PutMapping("/{orderId}")
    boolean markOrderPaid(@PathVariable("orderId") Long orderId){
        return orderService.markOrderPaid(orderId);
    }

}
